package com.ruoyi.web.controller.system.fuw;

import java.text.CollationKey;
import java.text.Collator;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.fuw.domain.FuwMessage;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fuw.domain.FuwTasks;
import com.ruoyi.fuw.service.IFuwTasksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务记录Controller
 * 
 * @author ruoyi
 * @date 2021-11-03
 */
@RestController
@RequestMapping("/fuw/tasks")
public class FuwTasksController extends BaseController
{
    @Autowired
    private IFuwTasksService fuwTasksService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询任务记录列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuwTasks fuwTasks)
    {
        startPage();
        List<FuwTasks> list = new ArrayList<>();
        // 判断是否是超级管理员，否则只能看到自己的购物车
        if (!getUsername().equals("admin")) {
            fuwTasks.setMedicalId(getUsername());
        }
        list = fuwTasksService.selectFuwTasksList(fuwTasks);
        return getDataTable(list);
    }

    /**
     * 查询每日任务
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:list')")
    @GetMapping("/list/date")
    public AjaxResult findTasksByDate(FuwTasks fuwTasks)
    {
        List<FuwTasks> listAll = new ArrayList<>();
        List<FuwTasks> listMine = new ArrayList<>();
        List<FuwTasks> listWorks = new ArrayList<>();

        // 判断当前人员是否存在岗位
        String nowPost = sysUserService.selectUserPostGroup(getUsername());

        FuwTasks fuwTasksMine = new FuwTasks();
        fuwTasksMine.setMedicalId(getUsername());
        fuwTasksMine.setTasksWeek(fuwTasks.getTasksWeek());
        listMine = fuwTasksService.selectFuwTasksList(fuwTasksMine);
        listAll.addAll(listMine);

        if(nowPost != null && !nowPost.equals("")){
            FuwTasks fuwTasksWorks = new FuwTasks();
            fuwTasksWorks.setWorkId(nowPost);
            fuwTasksWorks.setTasksWeek(fuwTasks.getTasksWeek());
            listWorks = fuwTasksService.selectFuwTasksList(fuwTasksWorks);
            listAll.addAll(listWorks);
        }

        Collections.sort(listAll, new Comparator<FuwTasks>() {
            Collator collator = Collator.getInstance(Locale.CHINA);
            @Override
            public int compare(FuwTasks o1, FuwTasks o2) {
                CollationKey key1 = collator.getCollationKey(String.valueOf(o1.getTasksStartTime().getTime()));
                CollationKey key2 = collator.getCollationKey(String.valueOf(o2.getTasksStartTime().getTime()));
                return key1.compareTo(key2);
            }
        });

        return AjaxResult.success(listAll);
    }

    /**
     * 查询医疗工作者medical/sysUser信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:list')")
    @GetMapping("/list/day")
    public AjaxResult listTasksDay()
    {
        // 查询每天的任务数
        List<FuwTasks> tasksList = fuwTasksService.seletTaskByDay();

        // 过滤掉正在服务状态的岗位组别
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for(FuwTasks task:tasksList){
            Map<String, Object> mapNew = new HashMap<>();
            mapNew.put("workingDay", task.getTasksWeek());
            if(task.getTasksNum() == 0){
                mapNew.put("content", "尚未布置");
                mapNew.put("color", "danger");
            }else if(task.getTasksNum() == 4){
                mapNew.put("content", "任务满额");
                mapNew.put("color", "primary");
            }else{
                mapNew.put("content", "任务缺漏");
                mapNew.put("color", "warning");
            }

            listMap.add(mapNew);
        }

        return AjaxResult.success(listMap);
    }


    /**
     * 导出任务记录列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:export')")
    @Log(title = "任务记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FuwTasks fuwTasks)
    {
        List<FuwTasks> list = fuwTasksService.selectFuwTasksList(fuwTasks);
        ExcelUtil<FuwTasks> util = new ExcelUtil<FuwTasks>(FuwTasks.class);
        return util.exportExcel(list, "任务记录数据");
    }

    /**
     * 获取任务记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:query')")
    @GetMapping(value = "/{tasksId}")
    public AjaxResult getInfo(@PathVariable("tasksId") String tasksId)
    {
        return AjaxResult.success(fuwTasksService.selectFuwTasksByTasksId(tasksId));
    }

    /**
     * 新增任务记录
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:add')")
    @Log(title = "任务记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FuwTasks fuwTasks)
    {
        return toAjax(fuwTasksService.insertFuwTasks(fuwTasks));
    }

    /**
     * 修改任务记录
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:edit')")
    @Log(title = "任务记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FuwTasks fuwTasks)
    {
        return toAjax(fuwTasksService.updateFuwTasks(fuwTasks));
    }

    /**
     * 删除任务记录
     */
    @PreAuthorize("@ss.hasPermi('fuw:tasks:remove')")
    @Log(title = "任务记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tasksIds}")
    public AjaxResult remove(@PathVariable String[] tasksIds)
    {
        return toAjax(fuwTasksService.deleteFuwTasksByTasksIds(tasksIds));
    }
}
